﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Data.SqlClient

Partial Class calls
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		Dim conn As New SqlConnection("Data Source=sql.bluetonemedia.com;Initial Catalog=BlueToneMediaDB;Persist Security Info=True;User ID=BTMWiz;Password=esquel4ma@gic;Failover Partner=sqlfailover.bluetonemedia.com")
		Dim Qry As String
        Dim dv As New DataView
		Dim dv2 As New DataView
        Dim number As String

        number = ""

        Dim sConString As String = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER=bluetonemedia.dyndns.org; DATABASE=asteriskcdrdb; UID=tbm; PASSWORD=tbm; OPTION=3;PORT=3306"
        Dim oConnection As OdbcConnection = New OdbcConnection(sConString)
        Dim sSQL As String = "SELECT src, clid, dst, calldate FROM cdr WHERE src != '9107952280' ORDER BY calldate DESC LIMIT 100"
        Dim oDataAdapter As OdbcDataAdapter = New OdbcDataAdapter(sSQL, oConnection)
        Dim oDataSet As DataSet = New DataSet()
        oDataAdapter.Fill(oDataSet)
        oDataGrid.DataSource = oDataSet
        oDataGrid.DataBind()

        dv.Table = oDataSet.Tables(0)
		oDataSet.WriteXml(Server.MapPath("") & "/callxml.xml")

        Dim i As Integer
        Dim cut As Integer
		
        cut = 0

		Dim dub as String
		
		dub = ""
		
		
        For i = 0 To 99
            If Server.HtmlDecode(dv(i)("src")).Length > 3 And cut < 10 and Server.HtmlDecode(dv(i)("src")) <> dub Then
				
				Dim phone As String
		  
				phone = ""
		
				phone = Server.HtmlDecode(dv(i)("src"))
		
				Dim area, pre, ends As String
		
				area = ""
				pre = ""
				ends = ""
			
				If Left(phone, 1) = "+" Then
					area = Mid(phone, 3, 3)
					pre = Mid(phone, 6, 3)
					ends = Right(phone, 4)
				Else
					area = Mid(phone, 1, 3)
					pre = Mid(phone, 5, 3)
					ends = Right(phone, 4)
				End If
		
				Qry = "SELECT name FROM Companys WHERE Phone LIKE '%" & area & "%' AND Phone LIKE '%" & pre & "%' AND Phone LIKE '%" & ends & "%'"
				conn.Open()
				Dim Sample As New DataSet
				Dim Sql As New SqlDataAdapter(Qry, conn)
				Sql.Fill(Sample)
				dv2.Table = Sample.Tables(0)
				
				Sample.WriteXml(Server.MapPath("") & "/callxml2.xml")
				'Response.write(Server.HtmlDecode(dv2(0)("count")) & "<Br/>")
			
				conn.Close()
				
				'If Sample.Tables(0).Rows().Count = 0 Then
					number = number & Server.HtmlDecode(dv(i)("src")) & "`" & Server.HtmlDecode(dv(i)("clid")) & "`" & Server.HtmlDecode(dv(i)("dst")) & "`" & area & " " & pre & " " & ends & "|" & Sample.Tables(0).Rows().Count & "`" & Server.HtmlDecode(Format(dv(i)("calldate"), "ddd h:m tt")) & "~"
				'Else
					'number = number & Server.HtmlDecode(dv(i)("src")) & "`" & Server.HtmlDecode(dv2(0)("Name")) & "`" & Server.HtmlDecode(dv(i)("dst")) & "`1`" & Server.HtmlDecode(Format(dv(i)("calldate"), "ddd h:m tt")) & "~"
				
				'End If
				
                cut = cut + 1
				dub = Server.HtmlDecode(dv(i)("src"))
            ElseIf cut = 10 Then
                Exit For
            End If
        Next

		

        Response.Write(number)
        Response.ContentType = "text/plain"
        Response.Flush()
        Response.End()
    End Sub
End Class
